//普通商户模式下单
function unifiedorder(params) {
	if (objIsEmpty(params)) {
		console.log('参数为空');
		return null
	}
	if (objIsEmpty(params.orderId)) {
		console.log('orderId为空');
		return null
	}
	if (objIsEmpty(params.openid)) {
		console.log('openid为空');
		return null
	}
	if (objIsEmpty(params.amount)) {
		console.log('amount为空');
		return null
	}
	if (objIsEmpty(params.title)) {
		console.log('title为空');
		return null
	}
	return new Promise((resolve, reject) => {
		// 1.调用下单
		uniCloud.callFunction({
			name: 'zhy-mp-weixin',
			data: params,
			success(res) {
				console.log('统一下单成功:' + JSON.stringify(res));
				resolve(res)
			},
			fail(err) {
				console.log('统一下单失败:' + JSON.stringify(res));
				reject(err)
			},
			complete(res) {
				console.log('统一下单完成:' + JSON.stringify(res));
			}
		})
	})
}

function objIsEmpty(obj) {
	if (obj == undefined || obj == '' || obj == null || JSON.stringify(obj) == '{}') {
		return true
	}
	return false
}

function businessRecharge(data) {
	return new Promise((resolve, reject) => {
		wx.requestPayment({
			timeStamp: data.timeStamp,
			nonceStr: data.nonceStr,
			package: data.package,
			signType: data.signType,
			paySign: data.paySign,
			success: function(res) {
				console.log('支付成功...');
				console.log(JSON.stringify(res));
				resolve(1)
			},
			fail: function(res) {
				console.log('支付失败');
				console.log(JSON.stringify(res));
				reject(res)
			},
			complete: function(res) {
				console.log('支付完成');
				console.log(JSON.stringify(res));
			}
		})
	})
}

function userWithdrawToCash(params) {
	const app = getApp()
	if (objIsEmpty(params)) {
		console.log('参数为空');
		return null
	}
	if (objIsEmpty(params.out_batch_no)) {
		console.log('out_batch_no为空');
		return null
	}
	if (objIsEmpty(params.batch_name)) {
		console.log('batch_name为空');
		return null
	}
	if (objIsEmpty(params.batch_remark)) {
		console.log('batch_remark为空');
		return null
	}
	if (objIsEmpty(params.total_amount)) {
		console.log('total_amount为空');
		return null
	}

	if (objIsEmpty(params.total_num)) {
		console.log('total_num为空');
		return null
	}
	if (objIsEmpty(params.transfer_detail_list) || params.transfer_detail_list.length == 0) {
		console.log('transfer_detail_list为空');
		return null
	}
	params.type = 3
	return new Promise((resolve, reject) => {
		// 1.调用提现:商家转账到零钱
		uniCloud.callFunction({
			name: 'zhy-mp-weixin',
			data: params,
			success(res) {
				console.log('提现成功:' + JSON.stringify(res));
				// 添加到用户提现记录表
				addWithdrawRecord(params, 1)
				// todo lxw 补贴结束，取消排队和补贴
				resolve(res)
			},
			fail(err) {
				console.log('提现失败:' + JSON.stringify(err));
				addWithdrawRecord(params, 0)
				reject(err)
			},
			complete(res) {
				console.log('提现完成:' + JSON.stringify(res));
			}
		})
	})
}

function addWithdrawRecord(withdrawRecord, success) {
	const app = getApp()
	withdrawRecord.success = success
	withdrawRecord.withdraw_time = new Date().getTime()
	withdrawRecord.openid = uni.getStorageSync(app.globalData.session.userKey)
	withdrawRecord.plat = 'mp-wexin'
	withdrawRecord.operateType = 1 // 新增

	console.log(JSON.stringify(withdrawRecord));
	uniCloud.callFunction({
		name: 'zhy-user-withdrawal-record',
		data: withdrawRecord,
		success(res) {
			console.log('添加提取记录成功');
			console.log(JSON.stringify(res));
		},
		fail(err) {
			console.log('添加提取记录失败');
			console.log(JSON.stringify(err));
		}
	})
}

//服务商模式模式下单：服务商模式都是以partner开头
function partnerUnifiedorder(params) {
	if (objIsEmpty(params)) {
		console.log('参数为空');
		return null
	}
	if (objIsEmpty(params.orderId)) {
		console.log('orderId为空');
		return null
	}
	if (objIsEmpty(params.openid)) {
		console.log('openid为空');
		return null
	}
	if (objIsEmpty(params.amount)) {
		console.log('amount为空');
		return null
	}
	if (objIsEmpty(params.amountFen)) {
		console.log('amountFen为空');
		return null
	}
	if (objIsEmpty(params.title)) {
		console.log('title为空');
		return null
	}
	params.type = 4 //统一下单：分账
	return new Promise((resolve, reject) => {
		// 1.调用下单
		uniCloud.callFunction({
			name: 'zhy-mp-weixin',
			data: params,
			success(res) {
				console.log('分账统一下单成功:' + JSON.stringify(res));
				resolve(res)
			},
			fail(err) {
				console.log('分账统一下单失败:' + JSON.stringify(res));
				reject(err)
			}
		})
	})
}

function partnerPay(data) {
	return new Promise((resolve, reject) => {
		wx.requestPayment({
			timeStamp: data.timeStamp,
			nonceStr: data.nonceStr,
			package: data.package,
			signType: data.signType,
			paySign: data.paySign,
			success: function(res) {
				console.log('支付成功...');
				console.log(JSON.stringify(res));
				resolve(1)
			},
			fail: function(res) {
				console.log('支付失败');
				console.log(JSON.stringify(res));
				reject(res)
			},
			complete: function(res) {
				console.log('支付完成');
				console.log(JSON.stringify(res));
			}
		})
	})
}

// 发起分账：添加分账接收者
function partnerProfitSharing(params) {
	if (objIsEmpty(params)) {
		console.log('参数为空');
		return 
	}
	params.type = 5 // 请求分账
	uniCloud.callFunction({
		name: 'zhy-mp-weixin',
		data: params,
		success(res) {
			console.log('发起分账成功');
			console.log(JSON.stringify(res));
		},
		fail(err) {
			console.log('发起分账失败');
			console.log(JSON.stringify(err));
		}
	})
}
// 查询分账结果
/* function partnerProfitSharing(params) {
	if (objIsEmpty(params)) {
		console.log('参数为空');
		return 
	}
	params.type = 5 // 请求分账
	uniCloud.callFunction({
		name: 'zhy-mp-weixin',
		data: params,
		success(res) {
			console.log('发起分账成功');
			console.log(JSON.stringify(res));
		},
		fail(err) {
			console.log('发起分账失败');
			console.log(JSON.stringify(err));
		}
	})
} */

module.exports = {
	unifiedorder,
	businessRecharge,
	userWithdrawToCash,
	partnerUnifiedorder,
	partnerPay,
	partnerProfitSharing
}